package com.zhongxi.guoyuan.view.user;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.weigan.loopview.LoopView;
import com.weigan.loopview.OnItemSelectedListener;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.projectItem.ProjectItemData;
import com.zhongxi.guoyuan.entity.projectItem.ProjectItemResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.BaiDuApiTest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/3/31.
 */

public class StoreSetActivity extends BaseActivity {

    @Bind(R.id.ivBack)
    ImageView mIvBack;
    @Bind(R.id.tvTitle)
    TextView mTvTitle;
    @Bind(R.id.et_storeName)
    EditText mEtStoreName;
    @Bind(R.id.tv_business_category)
    TextView mTvBusinessCategory;
    @Bind(R.id.ll_business_category)
    LinearLayout mLlBusinessCategory;
    @Bind(R.id.et_openTime)
    EditText mEtOpenTime;
    @Bind(R.id.et_storePhone)
    EditText mEtStorePhone;
    @Bind(R.id.et_personPhone)
    EditText mEtPersonPhone;
    @Bind(R.id.et_storeAddress)
    EditText mEtStoreAddress;
    @Bind(R.id.et_storeDesc)
    EditText mEtStoreDesc;
    @Bind(R.id.bt_store_submit)
    Button mBtStoreSubmit;
    private String mType;
    private List<String> mList = new ArrayList<>();
    private int position = 0;
    private String mId;
    private String mMerchantsCode;
    private ArrayList<ProjectItemData> mData;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_store_set);
        ButterKnife.bind(this);
        mTvTitle.setText("会员注册");
        if (getIntent() != null) {
            mId = getIntent().getStringExtra("id");
            mMerchantsCode = getIntent().getStringExtra("merchantsCode");
        }
    }

    @Override
    protected void initData() {
        super.initData();
        mType = "catelist";
        getServer(Url.CATELIST, null, "", false, null, "get");
    }

    @OnClick({R.id.ivBack, R.id.ll_business_category, R.id.bt_store_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ivBack:
                finish();
                break;
            case R.id.ll_business_category:
                showSelectExpressage();
                break;
            case R.id.bt_store_submit:
                final String mStoreName = mEtStoreName.getText().toString().trim();
                final String mBusinessCategory = mTvBusinessCategory.getText().toString().trim();
                final String mOpenTime = mEtOpenTime.getText().toString().trim();
                final String mStorePhone = mEtStorePhone.getText().toString().trim();
                final String mPersonPhone = mEtPersonPhone.getText().toString().trim();
                final String mStoreAddress = mEtStoreAddress.getText().toString().trim();
                final String mStoreDesc = mEtStoreDesc.getText().toString().trim();
                if (TextUtils.isEmpty(mStoreName)) {
                    showShortToast("请输入您的店铺名称");
                } else if (TextUtils.isEmpty(mBusinessCategory)) {
                    showShortToast("请选择您的经营类别");
                } else if (TextUtils.isEmpty(mOpenTime)) {
                    showShortToast("请输入您的营业时间");
                } else if (TextUtils.isEmpty(mStorePhone)) {
                    showShortToast("请输入您的商铺电话");
                } else if (TextUtils.isEmpty(mPersonPhone)) {
                    showShortToast("请输入您的注册手机号");
                } else if (TextUtils.isEmpty(mStoreAddress)) {
                    showShortToast("请输入您的店铺地址");
                } else {
                    BaiDuApiTest.getLatitude(mStoreAddress, new BaiDuApiTest.OnLocationListener() {
                        @Override
                        public void getLocation(String lon, String lat) {
                            Map mMap = new HashMap();
                            mMap.put("longitude", lon);
                            mMap.put("latitude", lat);
                            mMap.put("merchantsCode", mMerchantsCode);
                            mMap.put("useid", mId);
                            mMap.put("storename", mStoreName);
                            for (int i = 0; i < mData.size(); i++) {
                                ProjectItemData mProjectItemData = mData.get(i);
                                if (mBusinessCategory.equals(mProjectItemData.getName())) {
                                    mMap.put("cateid", mProjectItemData.getId());
                                }
                            }
                            mMap.put("phone", mStorePhone);
                            mMap.put("mphone", mPersonPhone);
                            mMap.put("address", mStoreAddress);
                            mMap.put("subject", mStoreDesc);
                            mMap.put("workertime", mOpenTime);
                            mType = "build";
                            getServer(Url.WXMERCHANTSBUILD, mMap, "", false, null, "post");
                        }

                        @Override
                        public void onError(String error) {
                            System.out.println("error==" + error);
                        }
                    });
                }
                break;
        }
    }

    private void showSelectExpressage() {
        final AlertDialog ad = new AlertDialog.Builder(context).create();
        View expressView = this.getLayoutInflater().inflate(R.layout.dialog_view, null);
        ad.setView(expressView);
        ad.setCanceledOnTouchOutside(false);
        ad.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                if (i == KeyEvent.KEYCODE_BACK && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
                    ad.dismiss();
                    return true;
                }
                return false;
            }
        });
        TextView tvNo = (TextView) expressView.findViewById(R.id.tv_no);
        TextView tvYes = (TextView) expressView.findViewById(R.id.tv_yes);
        LoopView loopView = (LoopView) expressView.findViewById(R.id.loopView);
        loopView.setItems(mList);
        loopView.setInitPosition(0);
        loopView.setNotLoop();
        loopView.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                position = index;
            }
        });

        tvNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ad.dismiss();
            }
        });
        tvYes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ad.dismiss();
                String s = mList.get(position);
                mTvBusinessCategory.setText(s);
            }
        });
        ad.show();

    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson mGson = new Gson();
        if ("catelist".equals(mType)) {
            ProjectItemResult mProjectItemResult = mGson.fromJson(response, ProjectItemResult.class);
            MyApplication.getInstance().setSpValue(mProjectItemResult.getSafeobj());
            if (mProjectItemResult.getData() != null && mProjectItemResult.getData().size() > 0) {
                mData = mProjectItemResult.getData();
                mList.clear();
                for (int i = 0; i < mProjectItemResult.getData().size(); i++) {
                    mList.add(mProjectItemResult.getData().get(i).getName());
                }
            }
        } else if ("build".equals(mType)) {
            BaseResponse mBaseResponse = mGson.fromJson(response, BaseResponse.class);
            MyApplication.getInstance().setSpValue(mBaseResponse.getSafeobj());
            Intent mIntent = new Intent(this, SetPasswordActivity.class);
            mIntent.putExtra("id", mBaseResponse.getId());
            startActivity(mIntent);
            finish();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson mGson = new Gson();
        BaseResponse mBaseResponse = mGson.fromJson(response, BaseResponse.class);
        MyApplication.getInstance().setSpValue(mBaseResponse.getSafeobj());
        if (mBaseResponse.getCode() == 1010) {
            showShortToast("店铺名或手机号已被抢注,请更换后重试");
        }
    }
}
